import 'package:dio/dio.dart' as dio;
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:xr_base/common.dart';

import '../data/user_data.dart';
import '../util/encrypt_util.dart';

class TokenInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    debugPrint('---😆😆TokenInterceptor0, onRequest, headers:${options.headers}😆😆---');

    /// userId
    options.headers["user-id"] = UserData().userId ?? '';

    /// user-type
    options.headers["user-type"] = '0';

    /// sign [secretKey+请求短链+token+userId+version+channelId 的md5加密]
    final _sign = EncryptUtil.md5String(('sijiao' +
        options.path +
        '${UserData().token ?? ''}' +
        '${UserData().userId ?? ''}' +
        Constant.server_version +
        Constant.channel_id));
    debugPrint('---😆😆TokenInterceptor, onRequest, _sign:${_sign}😆😆---');

    options.headers["sign"] = _sign;

    /// user-sex
    options.headers["user-sex"] = UserData().sex ?? '';

    ///todo get token from cache
    options.headers["token"] = UserData().token ?? '';

    debugPrint('---😆😆TokenInterceptor1, onRequest, headers:${options.headers}😆😆---');

    super.onRequest(options, handler);
  }

  @override
  void onResponse(dio.Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response, handler);
  }
}
